//
//  YSSettingView.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSSettingView.h"
#import "Masonry.h"
#import "YSReaderSetting.h"
#import "YSThemeCell.h"

@interface YSSettingView () <UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation YSSettingView {
    NSMutableArray *_scrollButtons;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    [self setupBrightnessView];

    [self setupFontView];

    [self setupThemeView];

    [self setupScrollTypeView];
}

- (void)setupBrightnessView {
    UIView *brightnessView = [[UIView alloc] init];
    brightnessView.backgroundColor = [UIColor clearColor];
    [self addSubview:brightnessView];

    [brightnessView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];

    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.text = @"亮度";
    label.textAlignment = NSTextAlignmentCenter;
    [brightnessView addSubview:label];

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        make.centerY.mas_equalTo(0);
    }];

    UIButton *followSystemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [followSystemButton setTitle:@"跟随系统" forState:UIControlStateNormal];
    followSystemButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [followSystemButton addTarget:self action:@selector(followSystemButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addButtonLayer:followSystemButton];
    [brightnessView addSubview:followSystemButton];

    [followSystemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.centerY.mas_equalTo(0);
    }];

    UISlider *slider = [[UISlider alloc] init];
    slider.minimumValue = 0;
    slider.maximumValue = 1;
    slider.value = 0.3;
    slider.minimumTrackTintColor = [UIColor orangeColor];
    slider.maximumTrackTintColor = [UIColor whiteColor];
    slider.thumbTintColor = [UIColor orangeColor];
    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    [brightnessView addSubview:slider];

    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right).offset(0);
        make.right.mas_equalTo(followSystemButton.mas_left).offset(-15);
        make.centerY.mas_equalTo(0);
    }];
}

- (void)setupFontView {
    UIView *fontView = [[UIView alloc] init];
    fontView.backgroundColor = [UIColor clearColor];
    [self addSubview:fontView];

    [fontView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];

    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.text = @"字体";
    label.textAlignment = NSTextAlignmentCenter;
    [fontView addSubview:label];

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.top.mas_equalTo(0);
    }];

    CGFloat buttonWidth = ([UIScreen mainScreen].bounds.size.width - 30 * 3 - 15 * 2 - 60) / 4;
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        if (i == 0) {
            [button setTitle:@"字体+" forState:UIControlStateNormal];
        } else if (i == 1) {
            [button setTitle:@"字体-" forState:UIControlStateNormal];
        } else if (i == 2) {
            [button setTitle:@"行距+" forState:UIControlStateNormal];
        } else {
            [button setTitle:@"行距-" forState:UIControlStateNormal];
        }
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.tag = i;
        [button addTarget:self action:@selector(fontButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(60+15+i*(buttonWidth+30), 0, buttonWidth, 30);
        [self addButtonLayer:button];
        [fontView addSubview:button];
    }
}

- (void)setupThemeView {
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor clearColor];
    [self addSubview:backView];

    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60+40);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];

    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.text = @"主题";
    label.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:label];

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        make.centerY.mas_equalTo(0);
    }];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake(40, 40);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[YSThemeCell class] forCellWithReuseIdentifier:NSStringFromClass([YSThemeCell class])];
    [backView addSubview:collectionView];

    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(label.mas_right);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
    }];
}

- (void)setupScrollTypeView {
    UIView *scrollTypeView = [[UIView alloc] init];
    scrollTypeView.backgroundColor = [UIColor clearColor];
    [self addSubview:scrollTypeView];

    [scrollTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60+40+50);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];

    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor whiteColor];
    label.text = @"翻页";
    label.textAlignment = NSTextAlignmentCenter;
    [scrollTypeView addSubview:label];
    label.frame = CGRectMake(0, 0, 60, 50);

    CGFloat buttonWidth = ([UIScreen mainScreen].bounds.size.width - 30 * 3 - 15 * 2 - 60) / 4;
    NSArray *titles = @[@"仿真",@"左右",@"上下",@"无"];
    _scrollButtons = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.tag = i;
        [scrollTypeView addSubview:button];
        [_scrollButtons addObject:button];
        if (i == [YSReaderSetting shareReaderSettings].pageStyle) {
            button.selected = YES;
        }
        [button addTarget:self action:@selector(scrollButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(60+15+i*(buttonWidth+30), 10, buttonWidth, 30);
        [self addButtonLayer:button];
    }

    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmptyEvent)]];
}

- (void)addButtonLayer:(UIButton *)button {
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;
    button.layer.borderColor = button.isSelected ? [UIColor orangeColor].CGColor : [UIColor whiteColor].CGColor;
    [button setTitleColor:button.isSelected ? [UIColor orangeColor] : [UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)followSystemButtonDidClick:(UIButton *)btn {
    NSLog(@"跟随系统");
    btn.selected = !btn.selected;
}

- (void)sliderValueChange:(UISlider *)slider {
    NSLog(@"亮度改变: %f", slider.value);
}

- (void)fontButtonDidClick:(UIButton *)button {
    if (button.tag == 0) {
        NSLog(@"字体+");
        [YSReaderSetting shareReaderSettings].fontSize = [YSReaderSetting shareReaderSettings].fontSize + 1;
    } else if (button.tag == 1) {
        NSLog(@"字体-");
        [YSReaderSetting shareReaderSettings].fontSize = [YSReaderSetting shareReaderSettings].fontSize - 1;
    } else if (button.tag == 2) {
        NSLog(@"行距+");
        [YSReaderSetting shareReaderSettings].lineSpacing = [YSReaderSetting shareReaderSettings].lineSpacing + 1;
    } else {
        NSLog(@"行距-");
        [YSReaderSetting shareReaderSettings].lineSpacing = [YSReaderSetting shareReaderSettings].lineSpacing - 1;
    }
}

- (void)scrollButtonDidClick:(UIButton *)button {
    if (button.isSelected) { return; }

    for (UIButton *b in _scrollButtons) {
        b.selected = NO;
        [self addButtonLayer:b];
    }

    button.selected = YES;
    [self addButtonLayer:button];

    NSArray *titles = @[@"仿真",@"左右",@"上下",@"无"];
    NSLog(@"%@", titles[button.tag]);
    [YSReaderSetting shareReaderSettings].pageStyle = button.tag;
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [YSReaderSetting shareReaderSettings].themeImageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YSThemeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YSThemeCell class]) forIndexPath:indexPath];
     cell.themeImageView.image = [UIImage imageNamed:[YSReaderSetting shareReaderSettings].themeImageArr[indexPath.row]];
    if (indexPath.row == [YSReaderSetting shareReaderSettings].theme) {
        cell.selectImageView.hidden = NO;
    } else {
        cell.selectImageView.hidden = YES;
    }

    cell.selectBlock = ^{
        [YSReaderSetting shareReaderSettings].theme = indexPath.row;
        [collectionView reloadData];
    };

    return cell;
}

- (void)tapEmptyEvent {
}

@end
